#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 305;

int n, m;
int s[N];
vector<int> ch[N];
int dp[N][N];
void dfs(int u) {
  dp[u][1] = s[u];
  for (int v : ch[u]) {
    dfs(v);
    per(i, m + 1, 2) {
      rep(j, 1, i - 1) { dp[u][i] = max(dp[u][i], dp[u][i - j] + dp[v][j]); }
    }
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) {
    int f;
    cin >> f >> s[i];
    ch[f].push_back(i);
  }
  dfs(0);
  cout << dp[0][m + 1];
  return 0;
}